package c2.cross.business.ladder.rank;

import c2.cross.business.ladder.common.LadderOppoDetail;
import cate.game.pvp.ladder.common.*;
import cate.game.pvp.ladder.data.LadderRank;

import java.util.ArrayList;
import java.util.List;

public class LadderRankDo {

    public LadderLoadOppoPsResult getRankById(LadderRankModule ladderRankModule, String query) {
        LadderLoadOppoPsResult result = new LadderLoadOppoPsResult();
        int rank = ladderRankModule.getRankNo(query);
        result.rank = rank;
        result.success();
        return result;
    }

    public LadderRankUpdateResult updateRank(LadderRankModule ladderRankModule, LadderRankUpdateQuery query) {
        LadderRankUpdateResult result = new LadderRankUpdateResult();
        ladderRankModule.updateRank(query);
        result.success();
        return result;
    }

    public LadderRankListResult getRank(LadderRankModule ladderRankModule, LadderRankListQuery query) {
        LadderRankListResult result = new LadderRankListResult();
        List<LadderOppoDetail> list = ladderRankModule.getList();
        if (query.limit <= 0) {
            //默认排行榜数为256
            query.limit = 256;
        }
        LadderOppoDetail selfDetail = ladderRankModule.getOppoDetail(query.roleId);
        LadderRank self = new LadderRank();
        int myRank = 0;
        if (selfDetail != null) {
            myRank = selfDetail.getRank();
            self.owner = selfDetail.rolePs.snapshot;
            self.value = selfDetail.rank;
            self.admireNum = selfDetail.admireNum;
        }
        result.myRank = myRank;
        result.self = self;
        List<LadderRank> ranklist = new ArrayList<>();
        for (LadderOppoDetail ladderOppoDetail : list) {
            LadderRank rank = new LadderRank();
            if (query.limit <= 0) {
                //默认排行榜数为256
                query.limit = 256;
            }
            if (ladderOppoDetail.rank <= query.limit) {
                rank.owner = ladderOppoDetail.rolePs.snapshot;
                rank.value = ladderOppoDetail.rank;
                rank.admireNum = ladderOppoDetail.admireNum;
                ranklist.add(rank);
            }
        }
        result.list = ranklist;
        result.roleUid = query.roleId;
        result.success();
        return result;
    }

    public LadderAdmireResult admireRank(LadderRankModule ladderRankModule, LadderRankAdmireQuery query) {
        LadderAdmireResult result = new LadderAdmireResult();
        int rank = query.rank - 1;
        LadderOppoDetail detail = ladderRankModule.getList().get(rank);
        if (detail == null) {
            result.fail("点赞失败，不存在这个排名="+query.rank);
            return result;
        }
        detail.admireNum ++;
        result.success();
        return result;
    }
}
